package com.xtgg.main;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.loopj.android.http.RequestParams;
import com.umeng.analytics.MobclickAgent;
import com.xtgg.R;
import com.xtgg.login.LoginActivity;
import com.xtgg.manager.BaseActivity;
import com.xtgg.model.Resp;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.TLog;
import com.xtgg.util.ToastUtil;
import com.xtgg.util.Utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class SearchMapActivity extends BaseActivity implements OnClickListener {
	MapView mMapView = null;
	BaiduMap mBaiduMap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_map);

		((TextView) findViewById(R.id.center)).setText("小兔帮助");
		MobclickAgent.onEvent(this, ((TextView) findViewById(R.id.center))
				.getText().toString());

		findViewById(R.id.left).setOnClickListener(this);

		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		mBaiduMap.setMyLocationEnabled(true);
		
		MapStatusUpdate u = MapStatusUpdateFactory.zoomTo(4);
		mBaiduMap.animateMapStatus(u);
		/*
		MyLocationData locData = new MyLocationData.Builder()
		.accuracy(1000).direction(100)
		.latitude(((DemoApp)this.getApplicationContext()).mLat)
		.longitude(((DemoApp)this.getApplicationContext()).mLng).build();
		mBaiduMap.setMyLocationData(locData);**/
		
		sendHttpRequest();
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.left:
			finish();
			break;
		}
	}
	
	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				TLog.d(TAG, "response=" + response);
				Resp ruser = JSON.parseObject(response, Resp.class);

				if (ruser.getState().equals("1")) {
					
					JSONObject obj = JSON.parseObject(response).getJSONArray("data").getJSONObject(0);
					
					String html = "<font color='#5b6778'>全国</font>";
					html += "<font color='#61aa35'>" + obj.getString("count_num") + "</font>";
					html += "<font color='#5b6778'>名小兔乖乖用户将会帮助您自动寻人。比上次搜寻增加</font>";
					html += "<font color='#61aa35'>" + obj.getString("add_num") + "</font>";
					html += "<font color='#5b6778'>人。</font>";
					
					((TextView)findViewById(R.id.tip)).setText(Html.fromHtml(html));
					
					final JSONArray array = obj.getJSONArray("detail");
					final Handler handle = new Handler()
					{
						public void handleMessage(Message msg)
						{
							JSONObject item = array.getJSONObject(msg.what);
							if(item.getString("latitude").length() == 0 || item.getString("latitude").length() == 0) return;
							
							LatLng point = new LatLng(item.getDoubleValue("latitude"),item.getDoubleValue("longitude"));  
							BitmapDescriptor bitmap = BitmapDescriptorFactory  
							    .fromResource(R.drawable.ic_map_red);  
							//构建MarkerOption，用于在地图上添加Marker  
							OverlayOptions option = new MarkerOptions()  
							    .position(point)  
							    .icon(bitmap);  
							mBaiduMap.addOverlay(option);
						}
					};
					
					new Thread()
					{
						public void run()
						{
							int count = array.size();
							for(int i = 0; i < count; i++)
							{
								handle.sendEmptyMessage(i);
								try 
								{
									Thread.sleep(10);
								} 
								catch (InterruptedException e) {
									e.printStackTrace();
								}
							}
						}
					}.start();
				}
				else if (ruser.getState().equals("2")) 
				{showExitDialog();} 
				else {
					ToastUtil.showToastWithAlertPic(ruser.getMessage());
				}
			}
		};
		// -----------------------
//{"data":[{"add_num":"0","count_num":"4","detail":[{"latitude":"","longitude":"","userid":"1000000010"},{"latitude":"0.0","longitude":"0.0","userid":"1000000011"},{"latitude":"","longitude":"","userid":"1000000012"},{"latitude":"","longitude":"","userid":"1000000013"}]}],"message":"查询信息列表成功！","state":"1"}
		RequestParams params = new RequestParams();
		params.put("act", "getHelp");
		params.put("time", Utils.ConverToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
		MgqRestClient.get(Constants.INFOMGT, params, loginHandler);
	}
}
